package org.mte.sakn.util;

import java.util.Date;

/**
 * Timer
 *
 * @author Marco Traversari
 * @version $Revision: 1.5 $ $Date: 2012-03-30 08:48:50 $
 */
public class Timer {

	private DateUtil dateUtil = null;

	/**
	 * startTime
	 */
	private long startTime = 0;

	/**
	 * stopTime
	 */
	private long stopTime = 0;

	/**
	 * elapsed
	 */
	private long elapsed  = 0;


	/**
	 * Timer
	 * @param pattern
	 */
	public Timer(String pattern) {
		dateUtil = new DateUtil(pattern);
	}

   /**
	 * getStartTime
	 * @return startTime
	 */
	public long getStartTime() {
		return startTime;
	}

	/**
	 * getStopTime
	 * @return stopTime
	 */
	public long getStopTime() {
		return stopTime;
	}

	/**
	 * getElapsed
	 * @return elapsed
	 */
	public long getElapsed() {
		return elapsed;
	}

    /**
     * Avvia cronometro
     */
    public void start() {
    	start("");
    }

    /**
     * Ferma cronometro
     */
   public void stop() {
	   stop("");
   }

   /**
    * start
    * @param label
    */
   public String start(String label) {
	   startTime = System.currentTimeMillis();
	   return label + "timer=start;time=" + dateUtil.getString(new Date(startTime));
   }

   /**
    * stop
    * @param label
    */
   public String stop(String label) {
	   stopTime = System.currentTimeMillis();
	   elapsed = stopTime - startTime;
   	   return label + "timer=stop;time=" + dateUtil.getString(new Date(stopTime)) + ";elapsed=" + elapsed;
   }

}
